import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { STComponent, STColumn, STChange, STRequestOptions, STData } from '@delon/abc/st';
import { SFComponent, SFSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SystemService } from '../../services/system.service';
import { SettingRoleEditComponent } from './edit/edit.component';

@Component({
  selector: 'app-role-management',
  templateUrl: './role-management.component.html',
  styleUrls: ['../../../commom/less/box.less']
})
export class RoleManagementComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;

  searchSchema: SFSchema = {
    properties: {
      roleName: {
        type: 'string',
        title: '角色名称',
        ui: { placeholder: '请输入' }
      }
    }
  };

  columns: STColumn[] = [
    {
      title: '序号',
      className: 'text-center',
      width: '80px',
      format: (item: STData, col: STColumn, idx: number) => {
        return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
      } 
    },
    { title: '角色名称', className: 'text-center', index: 'roleName' },
    { title: '角色描述', className: 'text-center', index: 'roleDescription' },
    { title: '企业数量', className: 'text-center', index: 'userNumber', iif: _ => this.type === 'freight' },
    { title: '创建人手机号', className: 'text-center', index: 'telephone' },
    {
      title: '创建时间',
      width: 170,
      index: 'createTime',
      type: 'date',
      className: 'text-center'
    },
    {
      title: '操作',
      className: 'text-center',
      buttons: [
        {
          text: '编辑',
          click: item => this.roleAction(item),
          iif: item => item.roleName !== '超级管理员',
          acl: { ability: ['SYSTEM-ROLE-edit'] }
        },
        {
          text: '删除',
          click: item => this.deleteAction(item),
          iif: item => item.roleName !== '超级管理员',
          acl: { ability: ['SYSTEM-ROLE-delete'] }
        }
      ]
    }
  ];

  type = 'user';
  params: any = {
    listUrl: this.service.$api_get_role_page,
    deleteUrl: this.service.$api_dalete_role,
    infoUrl: this.service.$api_getRoleInfo,
    addUrl: this.service.$api_save_role,
    updateUrl: this.service.$api_update_role,
    title: '角色管理',
    type: 'user'
  };

  constructor(public service: SystemService, private nzModalService: NzModalService, private route: ActivatedRoute) {
    route.params.subscribe(({ type }) => {
      if (type) {
        this.type = type;
        if (type !== 'user') {
          this.params = {
            listUrl: this.service.$api_get_ent_role_page,
            deleteUrl: this.service.$api_dalete_role,
            infoUrl: this.service.$api_getRoleInfo,
            addUrl: this.service.$api_save_role,
            updateUrl: this.service.$api_update_role,
            appId: 'A48F72F0A304427F921794BAD86B3522',
            title: '企业角色管理',
            type: 'freight'
          };
        }
      }
    });
  }

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf?.value });
    }
    if (requestOptions.body?.createTime) {
      Object.assign(requestOptions.body, { sort: 'createTime.' + requestOptions.body.createTime });
    }
    return requestOptions;
  };

  roleAction(item?: any) {
    const modal = this.nzModalService.create({
      nzContent: SettingRoleEditComponent,
      nzWidth: 900,
      nzComponentParams: item ? { params: { ...item, ...this.params } } : { params: { id: 0, ...this.params } },
      nzFooter: null
    });
    modal.afterClose.subscribe(res => {
      if (res) {
        this.st.load();
      }
    });
  }

  deleteAction(item: any) {
    this.nzModalService.error({
      nzTitle: '确认删除？',
      nzClosable: false,
      nzCancelText: '取消',
      nzOnOk: () => {
        this.service.request(this.params.deleteUrl, [item.id]).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('删除角色成功');
            this.st.load();
          }
        });
      }
    });
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }
}
